package com.cndw.kungfu.model.main;

import java.util.Collection;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;
import com.cndw.rpg.framework.SessionHolder;

/**
 * tcp协议的广播
 * @author fantadust
 * @date 2011-4-29 上午11:08:22
 */
@Component
public class TcpServer {

	/**
	 * 服务器session
	 */
	private SessionHolder sessionHolder = SessionHolder.getInstance();
	
	public Set<Long> allOnLineIds(){
		return sessionHolder.onlineIds();
	}
	
	
	private ResponseMessage makeResponseMessage(Object obj,String actionName){
		return makeResponseMessage(obj,ConstantsRadio.RADIO_MODULE, actionName);
	}
	
	private ResponseMessage makeResponseMessage(Object obj,String moduleName,String actionName){
		ResponseMessage message = null;
		if (obj instanceof ResponseMessage) {
			message = (ResponseMessage) obj;					
		}else{
			message = ResponseMessage.getSuccMessage(obj);
		}
		if (message.module == null )message.setModuleAndAction(moduleName, actionName);
		return message;		
	}
	
	
	/**
	 * 邀请确认框
	 * @param title 标题
	 * @param m 执行对象
	 * @param a 执行方法
	 * @param comfirmType 确认类型 //pt = 1 确定 2 取消   3 全提交
	 * @param parameters 参数
	 * @param ids
	 */
	public void broadcastConfirm(String title,String m,String a,int comfirmType,Object parameters,Long... ids){
		ResponseMessage message = ResponseMessage.confirmMsg(title, m, a, comfirmType, parameters);
		message.setModuleAndAction(ConstantsRadio.RADIO_MODULE, ConstantsRadio.inviteConfirmObj);
		broadcastResMsg(message, ids);
	}
	
	public void broadcastConfirm(String title,String m,String a,int comfirmType,Object parameters,String idKey){
		ResponseMessage message = ResponseMessage.confirmMsg(title, m, a, comfirmType, parameters);
		message.setModuleAndAction(ConstantsRadio.RADIO_MODULE, ConstantsRadio.inviteConfirmObj);
		broadcastResMsg(message, idKey);
	}
	
	/**
	 * 对执行ids发送消息
	 * @param obj 消息内容
	 * @param actionName 消息方法
	 * @param ids 用户playerId
	 */
	public void broadcast(Object obj,String actionName, Long... ids){
		ResponseMessage message = makeResponseMessage(obj,actionName);
		broadcastResMsg(message,ids);
	}	
	
	/**
	 * 对指定id发送消息
	 * @param message 消息内容
	 * @param ids 用户playerId 
	 */
	public void broadcastResMsg(ResponseMessage message,Long... ids){		
		sessionHolder.sendMessage(message, ids);
	}
	/**
	 * 集合广播
	 * @param obj
	 * @param actionName
	 * @param ids
	 */
	public void broadcast(Object obj,String actionName,Collection<Long> ids){
		ResponseMessage message = makeResponseMessage(obj,actionName);
		broadcastResMsg(message,ids);	
	}
	public void broadcastResMsg(ResponseMessage message,Collection<Long> ids){
		sessionHolder.sendMessage(message, ids);
	}
	
	
	
	public void broadcast(final Object obj,final String actionName,int pushTime,final Long... ids){
		if (pushTime == 0 || pushTime <= TimeProcesser.getUnixTime()) {
			broadcast(obj, actionName, ids);
		}else {
			Runnable r = new Runnable() {				
				@Override
				public void run() {
					broadcast(obj, actionName, ids);
				}
			};
			Context.getTimerServer().scheduleSeconds(r, pushTime - TimeProcesser.getUnixTime());
		}
	}
	
	public void broadcast(final Object obj,final String actionName,int pushTime,final Collection<Long> ids){
		if (pushTime == 0 || pushTime <= TimeProcesser.getUnixTime()) {
			broadcast(obj, actionName, ids);
		}else {
			Runnable r = new Runnable() {				
				@Override
				public void run() {
					broadcast(obj, actionName, ids);
				}
			};
			Context.getTimerServer().scheduleSeconds(r, pushTime - TimeProcesser.getUnixTime());
		}
	}
	
	public void broadcast(final Object object,final String actionName,final String idKey,int delay){
		if (delay<=0) {
			broadcast(object,actionName,idKey);
		}else {
			Runnable r = new Runnable() {				
				@Override
				public void run() {
					broadcast(object,actionName,idKey);
				}
			};
			Context.getTimerServer().scheduleSeconds(r, delay);
		}
	}

	
	/**
	 * 发送组播
	 * @param obj 广播内容
	 * @param actionName 广播方法名
	 * @param idKey 组播key
	 */
	public void broadcast(Object obj,String actionName, String idKey){
		ResponseMessage message = makeResponseMessage(obj,actionName);
		broadcastResMsg(message,idKey);			
	}
	
	/**
	 * 发送组播
	 * @param responseMessage 组播消息体
	 * @param idKey 组播key
	 */
	public void broadcastResMsg(ResponseMessage message,String idKey){
		sessionHolder.radioGroupMsg(idKey, message);		
	}	
	
	/**
	 * 把一个用户加入一个组
	 * @param session
	 * @param idKey
	 */
	public void addGroup(Session session,String idKey){
		sessionHolder.addToRadioGroup(idKey, session);
	}
	/**
	 * 把一个用户从某个组移除
	 * @param session
	 * @param idKey
	 */
	public void removeGroup(Session session,String idKey){
		sessionHolder.removeFromRadioGroup(idKey, session);
	}	

	
}
